package com.tanhua.server.service;

import com.tanhua.server.mapper.QuestionnaireMapper;
import com.tanhua.server.pojo.Exam;
import com.tanhua.server.pojo.Option;
import com.tanhua.server.pojo.Questionnaire;
import com.tanhua.server.vo.OptionVo;
import com.tanhua.server.vo.QuestionVo;
import com.tanhua.server.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionnaireService {

    @Autowired
    private QuestionnaireMapper questionnaireMapper;




    public List<ResultVo> queryByType(int type, String reportId) {
        List<ResultVo> list = new ArrayList<>();
        switch (type){
            //代表用户是第一次做题，解锁初级题目。
            case 0:
                ResultVo voFirst = fillPrimaryQuestion("1");
                ResultVo voSecond = fillPrimaryQuestion("2");
                voSecond.setIsLock(1);
                ResultVo voThird = fillPrimaryQuestion("3");
                voThird.setIsLock(1);
                list.add(voFirst);
                list.add(voSecond);
                list.add(voThird);
                break;
            //代表用户已经做完了初级题目，解锁中级题目
            case 1:
                ResultVo voFirst1 = fillPrimaryQuestion("1");
                voFirst1.setReportId(reportId);
                ResultVo voSecond1 = fillPrimaryQuestion("2");
                voSecond1.setIsLock(0);
                ResultVo voThird1 = fillPrimaryQuestion("3");
                voThird1.setIsLock(1);
                list.add(voFirst1);
                list.add(voSecond1);
                list.add(voThird1);
                break;
            //代表用户做过了中级题，解锁全部的题
            case 2:
                ResultVo voFirst2 = fillPrimaryQuestion("1");
                ResultVo voSecond2 = fillPrimaryQuestion("2");
                voSecond2.setReportId(reportId);
                voSecond2.setIsLock(0);
                ResultVo voThird2 = fillPrimaryQuestion("3");
                voThird2.setIsLock(0);
                list.add(voFirst2);
                list.add(voSecond2);
                list.add(voThird2);
                break;
            default:
                break;
        }
        return list;
    }

    /**
     * 填充题
     * @return
     */
    private ResultVo fillPrimaryQuestion(String type) {

        Questionnaire questionnaire = this.questionnaireMapper.selectById(type);
        ResultVo resultVo = new ResultVo();
        resultVo.setId(questionnaire.getId());
        resultVo.setCover(questionnaire.getCover());
        resultVo.setLevel(questionnaire.getLevel());
        resultVo.setName(questionnaire.getName());
        resultVo.setStar(questionnaire.getStar());
        if("1".equals(type)){
            resultVo.setIsLock(0);
        }
        //查试卷
        Questionnaire questionnaire1 = this.questionnaireMapper.selectAll(type);
        List<QuestionVo> questionVoList = new ArrayList<>();

        //遍历获取到的题目
        for (Exam exam : questionnaire1.getExams()) {
            //封装题目Vo
            QuestionVo questionVo = new QuestionVo();
            questionVo.setId(exam.getId());
            questionVo.setQuestion(exam.getQuestion());

            //封装选项Vo
            List<OptionVo> optionVoList = new ArrayList<>();
            for (Option option : exam.getOptions()) {
                OptionVo optionVo = new OptionVo();
                optionVo.setId(option.getId());
                optionVo.setOption(option.getOption());
                optionVoList.add(optionVo);
            }
            questionVo.setOptions(optionVoList);
            questionVoList.add(questionVo);
        }
        resultVo.setQuestions(questionVoList);
        return resultVo;
    }

}
